/* 
 * Enterprise Library Extensions - DI + ORM.
 * Copyright (C) 2006
 *
 * GNU General Public License
 *
 * This program is free software; you can redistribute 
 * it and/or modify it under the terms of the GNU General Public License 
 * as published by the Free Software Foundation; either version 2 of the 
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but 
 * WITHOUT ANY WARRANTY; without even the implied warranty of 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
 * See the GNU General Public License for more details.
 */
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing.Design;
using Microsoft.Practices.EnterpriseLibrary.Configuration.Design;
using Microsoft.Practices.EnterpriseLibrary.Configuration.Design.Validation;
using ELE.EnterpriseLibrary.Data.Configuration.Design.Properties;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using System.Configuration;
using System.Reflection;

namespace ELE.EnterpriseLibrary.Data.Configuration.Design
{

  [Image(typeof(ObjectTypeDefNode))]
  [SelectedImage(typeof(ObjectTypeDefNode))]
  public class ObjectMappingReferenceNode : ConfigurationNode
  {
    private string mapName;
    private string objectName;

    private ObjectMappingNode mappingNode;
    private EventHandler<ConfigurationNodeChangedEventArgs> mappingNodeRemovedHandler;

    public ObjectMappingReferenceNode()
      : this(new MappingReferenceData())
    {
        
    }

    public ObjectMappingReferenceNode(MappingReferenceData data)
      : base((data == null) ? Resource.DefaultMappingReferenceDefName : data.Name + "." + data.ObjectId)
    {
      if (data == null) { throw new ArgumentNullException("MappingReferenceData"); }
      this.mappingNodeRemovedHandler = new EventHandler<ConfigurationNodeChangedEventArgs>(OnMappingNodeRemoved);
      this.mapName = data.Name;
      this.objectName = data.ObjectId;

      if (data.OverwriteBindings != null)
      {
        foreach (PropertyBindingOverwriteData binding in data.OverwriteBindings)
        {
          ObjectBindingOverwriteNode tNode = new ObjectBindingOverwriteNode(binding);
          this.AddNode(tNode);
        }
      }
    }
    [Browsable(false)]
    public string MapName
    {
      get { return this.mapName; }
    }
    [Browsable(false)]
    public string ObjectName
    {
      get { return this.objectName; }
    }
    [Browsable(false)]
    public MappingReferenceData MappingData
    {
      get
      {
        MappingReferenceData data = new MappingReferenceData();
        if (this.Mapping != null)
        {
          data.Name = this.Mapping.Parent.Name;
          data.ObjectId = this.Mapping.Name;
        }
        if (this.Nodes != null)
        {
          foreach (ObjectBindingOverwriteNode node in this.Nodes)
          {
            data.OverwriteBindings.Add(node.BindingOverwriteData);
          }
        }
	      return data;
      }
    }

    [Required]
    [SRDescription("ObjectMappingReferenceDescription", typeof(Resource))]
    [Editor(typeof(ReferenceEditor), typeof(UITypeEditor))]
    [ReferenceType(typeof(ObjectMappingNode))]
    [SRCategory("CategoryGeneral", typeof(Resource))]
    public ObjectMappingNode Mapping
    {
      get { return mappingNode; }
      set
      {
        mappingNode = LinkNodeHelper.CreateReference<ObjectMappingNode>(mappingNode,
                                                                    value,
                                                                    mappingNodeRemovedHandler,
                                                                    null);
        this.Name = mappingNode.Parent.Name + "." + mappingNode.Name;
      }
    }
    protected override void Dispose(bool disposing)
    {
      try
      {
        if (disposing)
        {
          if (null != mappingNode)
          {
            mappingNode.Removed -= mappingNodeRemovedHandler;
          }
        }
      }
      finally
      {
        base.Dispose(disposing);
      }
    }

    protected override void OnChildAdded(ConfigurationNodeChangedEventArgs e)
    {
      base.OnChildAdded(e);
      ObjectBindingOverwriteNode node = e.Node as ObjectBindingOverwriteNode;
      Debug.Assert(null != node, "Only ObjectBindingOverwriteNode objects can be added to a ObjectMappingReferenceNode");

    }
    private void OnMappingNodeRemoved(object sender, ConfigurationNodeChangedEventArgs e)
    {
      this.mappingNode = null;
    }

  }
}